import java.util.LinkedList;
import java.util.Queue;

public class Solution {
    public static int[] dx = {0,0,-1,1};
    public static int[] dy = {1,-1,0,0};
    int n;
    int m;
    public static boolean[][] b;

    public int solve (char[][] grid) {
        n = grid.length;
        m = grid[0].length;
        b = new boolean[n][m];
        int n1 = bfs(grid);
        return n1;
    }

    public int bfs(char[][] grid){
        Queue<int[]> queue = new LinkedList<>();
        int ret = 0;
        for(int i = 0; i < n; i++)
            for(int j = 0; j < m; j++){
                if(grid[i][j] == '1' && b[i][j] == false){
                    ret++;
                    b[i][j] = true; int[] a1 = {i,j};
                    queue.add(a1);
                    while (!queue.isEmpty()){
                        int[] a2 = queue.poll();
                        for(int k = 0; k < 4; k++){
                            int x = a2[0] + dx[k], y = a2[1] + dy[k];
                            if(x >= 0 && x < n && y >= 0 && y < m && grid[x][y] == '1' && b[x][y] == false){
                                int[] a3 = {x,y}; b[x][y] = true;
                                queue.add(a3);
                            }
                        }
                    }
                }
            }
        return ret;
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        char[][] c = {{'1','1','0','0','0'},{'1','0','0','1','1'},{'0','0','0','1','1'},{'0','0','0','0','0'},{'0','0','1','1','1'}};
        solution.solve(c);
    }
}
